package com.luhuiguo.cms.server.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;

import org.apache.commons.io.FilenameUtils;

import com.google.appengine.api.datastore.Blob;

/**
 * 附件
 * 
 * @author LuHuiguo
 * @version $Id: Attachment.java 188 2010-05-31 08:23:20Z luhuiguo@gmail.com $
 */
@Entity
public class Attachment implements Serializable {

    private static final long serialVersionUID = -702588431043137047L;

    private static final String[] IMAGE_EXTENSIONS = { "jpg", "jpeg", "png", "ico", "gif" };

    /**
     * 标识
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 标题
     */
    @Basic
    private String title;

    /**
     * 描述
     */
    @Basic
    private String description;

    /**
     * 文件名
     */
    @Basic
    private String filename;

    /**
     * 所属文章
     */
    @Basic
    private Long articleId;

    /**
     * 媒体类型
     */
    @Basic
    private String mimeType;

    /**
     * 文件内容
     */
    @Enumerated
    private Blob content;

    /**
     * 最后修改时间
     */
    @Basic
    private Date lastModified;

    /**
     * 文件大小
     */
    @Basic
    private int size;

    /**
     * 文件大小
     */
    @Basic
    private int type;

    /**
     * 顺序
     */
    @Basic
    private int rank;

    @Basic
    private int grade;

    /**
     * 下载次数
     */
    @Basic
    private int downloads;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public Long getArticleId() {
        return articleId;
    }

    public void setArticleId(Long articleId) {
        this.articleId = articleId;
    }

    public String getMimeType() {
        return mimeType;
    }

    public void setMimeType(String mimeType) {
        this.mimeType = mimeType;
    }

    public Blob getContent() {
        return content;
    }

    public void setContent(Blob content) {
        this.content = content;
    }

    public Date getLastModified() {
        return lastModified;
    }

    public void setLastModified(Date lastModified) {
        this.lastModified = lastModified;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public int getDownloads() {
        return downloads;
    }

    public void setDownloads(int downloads) {
        this.downloads = downloads;
    }

    @Transient
    public boolean isImage() {
        for (String ext : IMAGE_EXTENSIONS) {
            if (ext.equalsIgnoreCase(FilenameUtils.getExtension(getFilename()))) {
                return true;
            }
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Attachment [filename=").append(filename).append(", id=").append(id).append(", lastModified=")
                .append(lastModified).append(", mimeType=").append(mimeType).append(", size=").append(size).append(
                        ", title=").append(title).append("]");
        return builder.toString();
    }

}
